﻿using System;
using System.Text;
using OpenGameEngine.Base;
using OpenGameEngine.Scenes;
using OpenGameEngine.GameObjects;

namespace OpenGameEngine
{
    /// <summary>
    /// The game engine
    /// </summary>
    public class GameEngine
        : Game
    {
        /// <summary>
        /// Creates a new instance of the game engine
        /// </summary>
        public GameEngine()
        {
            _scenes = new GameSceneCollection();
        }

        private GameSceneCollection _scenes;
        /// <summary>
        /// The scenes in the game
        /// </summary>
        public GameSceneCollection Scenes { get { return _scenes; } }

        /// <summary>
        /// The current scene name
        /// </summary>
        public String SceneName
        {
            get { return _currScene.Name; }
        }

        private GameScene _currScene;
        /// <summary>
        /// The current scene object
        /// </summary>
        public GameScene CurrentScene
        {
            get { return _currScene; }
        }

        protected override void Initialize(GameConfiguration config)
        {
            base.Initialize(config);
        }

        protected override void Draw(GameTime time)
        {
            base.Draw(time);
            DrawEventArgs e = new DrawEventArgs(this, time);

            if (CurrentScene != null)
            {
                if (!CurrentScene.IsLoaded)
                    CurrentScene.Load(e);

                if (CurrentScene.IsLoaded)
                    CurrentScene.Draw(e);
            }

            e = null;
        }

        protected override void Update(GameTime time)
        {
            base.Update(time);
            GameCoreEventArgs e = new GameCoreEventArgs(this, time);

            if (CurrentScene != null)
            {
                if (!CurrentScene.IsLoaded)
                    CurrentScene.Load(e);

                if (CurrentScene.IsLoaded)
                    CurrentScene.Update(e);
            }

            e = null;

        }

        /// <summary>
        /// Switch to a scene
        /// </summary>
        /// <param name="sceneName">The scene name</param>
        public void SwitchScene(String sceneName)
        {
            GameCoreEventArgs e = new GameCoreEventArgs(this, this.clock);

            if (_currScene != null)
                _currScene.OnSceneEnded(e);

            if (Scenes.Contains(sceneName))
            {
                _currScene = Scenes[sceneName];
                _currScene.OnSceneEntered(e);
            }
        }

        protected override void GameDeactivated(GameTime time)
        {
            //GameCoreEventArgs e = new GameCoreEventArgs(this, time);
            base.GameDeactivated(time);

            //SwitchScene("Pause");
        }

        protected override void EndRun(GameTime time)
        {
            GameCoreEventArgs e = new GameCoreEventArgs(this, time);

            foreach (GameScene j in this.Scenes)
            {
                j.Close(e);
            }

            e = null;

            base.EndRun(time);
        }

    }
}
